package com.atguigu.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.app.func.DorisSinkFunction;
import com.atguigu.edu.realtime.app.func.MyAsyncFunction;
import com.atguigu.edu.realtime.beans.PlayChapterVideoBean;
import com.atguigu.edu.realtime.beans.TrafficPageViewBean;
import com.atguigu.edu.realtime.utils.DateFormatUtil;
import com.atguigu.edu.realtime.utils.MyKafkaUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.concurrent.TimeUnit;

//播放域各章节视频播放各窗口汇总表
//累计播放时长,观看人数,视频播放次数
public class DwsPlayChapterVideoWindow {
    public static void main(String[] args) {
        //基本环境准备
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(5);
        //检查点配置
        /*env.enableCheckpointing(5000l);
        env.getCheckpointConfig().setCheckpointTimeout(60000l);
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000l);
        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30),Time.seconds(10)));
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/edu/ck");
        System.setProperty("HADOOP_USER_NAME","atguigu");*/
        //读取kafka的dwd_traffic_page_log主题
        DataStreamSource<String> jsonStrDs = env.fromSource(MyKafkaUtil.getKafkaSource("dwd_traffic_video_log", "dws_traffic_video_group"),
                WatermarkStrategy.noWatermarks(), "kafkassorce");

        //转换数据类型
        SingleOutputStreamOperator<JSONObject> jsonObj = jsonStrDs.map(JSON::parseObject);

        //按照用户,视频id分组
        KeyedStream<JSONObject, Tuple2<String, String>> keyedDs = jsonObj.keyBy(
                new KeySelector<JSONObject, Tuple2<String, String>>() {
                    @Override
                    public Tuple2<String, String> getKey(JSONObject jsonObj) throws Exception {
                        return Tuple2.of(jsonObj.getJSONObject("common").getString("uid")
                                , jsonObj.getJSONObject("appVideo").getString("video_id"));
                    }
                }
        );
        //封装为bean
        SingleOutputStreamOperator<PlayChapterVideoBean> beanDs = keyedDs.process(
                new ProcessFunction<JSONObject, PlayChapterVideoBean>() {

                    private ValueState<String> playState;
                    private ValueState<String> userDate;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        ValueStateDescriptor<String> playStateProperties = new ValueStateDescriptor<String>("playState", String.class);
                        playStateProperties.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(35)).build());
                        playState = getRuntimeContext().getState(playStateProperties);

                        ValueStateDescriptor<String> userstateProperties = new ValueStateDescriptor<String>("userDate", String.class);
                        userstateProperties.enableTimeToLive(StateTtlConfig.newBuilder(Time.days(1)).build());
                        userDate = getRuntimeContext().getState(userstateProperties);
                    }

                    @Override
                    public void processElement(JSONObject jsonObj, ProcessFunction<JSONObject, PlayChapterVideoBean>.Context ctx, Collector<PlayChapterVideoBean> out) throws Exception {
                        JSONObject common = jsonObj.getJSONObject("common");
                        JSONObject appVideo = jsonObj.getJSONObject("appVideo");
                        Long ts = jsonObj.getLong("ts");

                        String videoId = appVideo.getString("video_id");
                        Long playSec = appVideo.getLong("play_sec");

                        Long playSum = 0l;
                        String playValue = playState.value();
                        //判断视频播放次数
                        if (StringUtils.isEmpty(playValue)) {
                            playSum = 1l;
                            playState.update("1");
                        }

                        Long userSum = 0l;
                        //观看人数
                        String userValue = userDate.value();
                        String date = DateFormatUtil.toDate(ts);
                        if (StringUtils.isEmpty(userValue) || !date.equals(userSum)) {
                            userDate.update(date);
                            userSum = 1l;
                        }

                        out.collect(
                                new PlayChapterVideoBean(
                                        "",
                                        "",
                                        videoId,
                                        "",
                                        "",
                                        "",
                                        playSum,
                                        playSec,
                                        userSum,
                                        ts
                                )
                        );

                    }
                }
        );
        //关联维度
        SingleOutputStreamOperator<PlayChapterVideoBean> joinVideoDs = AsyncDataStream.unorderedWait(
                beanDs,
                new MyAsyncFunction<PlayChapterVideoBean>("dim_video_info") {
                    @Override
                    public void join(JSONObject jsonObject, PlayChapterVideoBean orderBean) {
                        orderBean.setChapterId(jsonObject.getString("chapter_id"));
                    }

                    @Override
                    public String getKey(PlayChapterVideoBean orderBean) {
                        return orderBean.getVideoId();
                    }
                },
                60
                , TimeUnit.SECONDS
        );
        SingleOutputStreamOperator<PlayChapterVideoBean> joinChapterDs = AsyncDataStream.unorderedWait(
                joinVideoDs,
                new MyAsyncFunction<PlayChapterVideoBean>("dim_chapter_info") {
                    @Override
                    public void join(JSONObject jsonObject, PlayChapterVideoBean orderBean) {
                        orderBean.setChapterName(jsonObject.getString("chapter_name"));
                    }

                    @Override
                    public String getKey(PlayChapterVideoBean orderBean) {
                        return orderBean.getChapterId();
                    }
                },
                60
                , TimeUnit.SECONDS
        );

        //设定水位线,分组,开窗,聚合
        SingleOutputStreamOperator<PlayChapterVideoBean> reduceDs = joinChapterDs.assignTimestampsAndWatermarks(
                WatermarkStrategy.<PlayChapterVideoBean>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<PlayChapterVideoBean>() {
                                    @Override
                                    public long extractTimestamp(PlayChapterVideoBean element, long recordTimestamp) {
                                        return element.getTs();
                                    }
                                }
                        )
        ).keyBy(
                new KeySelector<PlayChapterVideoBean, String>() {
                    @Override
                    public String getKey(PlayChapterVideoBean value) throws Exception {
                        return value.getChapterName();
                    }
                }
        ).window(
                TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10))
        ).reduce(
                new ReduceFunction<PlayChapterVideoBean>() {
                    @Override
                    public PlayChapterVideoBean reduce(PlayChapterVideoBean value1, PlayChapterVideoBean value2) throws Exception {
                        value1.setPlaySum(value1.getPlaySum() + value2.getPlaySum());
                        value1.setTimeSum(value1.getTimeSum() + value2.getTimeSum());
                        value1.setUserSum(value1.getUserSum() + value2.getUserSum());
                        return value1;
                    }
                }
                , new ProcessWindowFunction<PlayChapterVideoBean, PlayChapterVideoBean, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<PlayChapterVideoBean, PlayChapterVideoBean, String, TimeWindow>.Context context, Iterable<PlayChapterVideoBean> elements, Collector<PlayChapterVideoBean> out) throws Exception {
                        String stt = DateFormatUtil.toYmdHms(context.window().getStart());
                        String edt = DateFormatUtil.toYmdHms(context.window().getEnd());
                        String date = DateFormatUtil.toDate(context.window().getStart());

                        PlayChapterVideoBean viewBean = elements.iterator().next();
                        viewBean.setStt(stt);
                        viewBean.setEdt(edt);
                        viewBean.setCurDate(date);

                        viewBean.setVideoId(null);

                        out.collect(viewBean);
                    }
                }
        );
        //写入到doris

        reduceDs.print(">>>>>");
        reduceDs.addSink(
                new DorisSinkFunction<PlayChapterVideoBean>("dws_play_chapter_video_window")
        );
        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
